package cn.itsource.pethome.utils;

import cn.itsource.pethome.domain.Shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 邮件发送的工具类
 */
@Component
public class EmailUtils {

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送激活邮件
     * @param shop
     */
    public void send(Shop shop){
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            //设置发送人【要与properties配置文件中的发件人邮箱一致】
            helper.setFrom("wuyong_0513@163.com");
            //邮件主题
            helper.setSubject("宠物之家-商家入驻激活");
            //邮件内容
            helper.setText("<h2>宠物之家-商家入驻激活</h2><br/><br/>" +
                    "恭喜你，您已成功入驻到宠物之家平台，点击以下链接即可激活您的账户！<br/><br/>" +
                    "<a href='http://localhost:8080/lr/active/"+shop.getId()+"' style='color:blue;font-size:20px;'>点击激活</a>",true);
            //收件人
            helper.setTo(shop.getAdmin().getEmail());
            //添加附件
            //helper.addAttachment("timg4.jpg", new File("C:/Users/Administrator/Desktop/img/timg4.jpg"));
            //抄送人
            //mailMessage.setCc();
            //密送人
            //mailMessage.setBcc();
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}
